﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.Model;
using VideoRentalShop.Controller;

namespace VideoRentalShop.View.Windows.ClientList
{
    /// <summary>
    /// Logika interakcji dla klasy SearchPanel.xaml
    /// </summary>
    public partial class SearchPanel : UserControl
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;


        public SearchPanel()
        {
            InitializeComponent();
        }


        public void Clear()
        {
            txtName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtPesel.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtAddressStreet.Text = string.Empty;
            txtAddressPostalCode.Text = string.Empty;
            txtAddressCity.Text = string.Empty;
        }

        public IEnumerable<Client> GetResults()
        {
            var name = txtName.Text.Trim().ToLower();
            var lastName = txtLastName.Text.Trim().ToLower();
            var pesel = txtPesel.Text.Trim().ToLower();
            var email = txtEmail.Text.Trim().ToLower();
            var street = txtAddressStreet.Text.Trim().ToLower();
            var postalCode = txtAddressPostalCode.Text.Trim().ToLower();
            var city = txtAddressCity.Text.Trim().ToLower();

            var results = _controller.Data.Client.GetAll() as IEnumerable<Client>;

            if (!string.IsNullOrWhiteSpace(name))
            {
                results = results.Where(x => x.Name.ToLower().Contains(name));
            }
            if (!string.IsNullOrWhiteSpace(lastName))
            {
                results = results.Where(x => x.LastName.ToLower().Contains(lastName));
            }
            if (!string.IsNullOrWhiteSpace(pesel))
            {
                results = results.Where(x => x.Pesel.ToLower().Contains(pesel));
            }
            if (!string.IsNullOrWhiteSpace(email))
            {
                results = results.Where(x => x.Email.ToLower().Contains(email));
            }
            if (!string.IsNullOrWhiteSpace(street))
            {
                results = results.Where(x => x.Street.ToLower().Contains(street));
            }
            if (!string.IsNullOrWhiteSpace(postalCode))
            {
                results = results.Where(x => x.PostalCode.ToLower().Contains(postalCode));
            }
            if (!string.IsNullOrWhiteSpace(city))
            {
                results = results.Where(x => x.City.ToLower().Contains(city));
            }

            return results;
        }
    }
}
